package org.advanced.reflection;

import java.lang.reflect.Constructor;

/**
 * @author John.zhang
 * @version Id: PersonSingle, v 1.0 2020/12/3 20:35 ZhiYong Exp $
 * 如何避免反射破坏单例
 */
public class PersonSingle {

    private static PersonSingle personSingle;

    private PersonSingle() {
        if (personSingle != null) {
            throw new RuntimeException("实例已经存在！");
        }
    }

    public static PersonSingle getInstance() {
        if (personSingle == null) {
            personSingle = new PersonSingle();
        }
        return personSingle;
    }


    public static void main(String[] args) throws Exception {
        Constructor<PersonSingle> declaredConstructor = PersonSingle.class.getDeclaredConstructor();
        PersonSingle personSingle = declaredConstructor.newInstance();
    }

}
